package com.hoshiicloud.mallshop.controller;

import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.constant.UserTypeEnum;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.DigestUtils;
import com.hoshiicloud.common.utils.token.UserInfoUtil;
import com.hoshiicloud.mallshop.feign.ShopAuthFeignService;
import com.hoshiicloud.mallshop.param.StoreLoginParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/8/8 9:38qry
 */
@RestController
@Slf4j
@Api(value = "店铺登录 控制器",tags = {"店铺登录 控制器"})
@RequestMapping("/shop-login")
public class ShopLoginController {
    private final RedisTemplate<String, Object> redisTemplate;
    private final ShopAuthFeignService authService;
    public ShopLoginController(RedisTemplate<String, Object> redisTemplate, ShopAuthFeignService authService) {
        this.redisTemplate = redisTemplate;
        this.authService = authService;
    }

    @ApiOperation(value = "店铺系统用户登录")
    @PostMapping("/login")
    public Response shopUserLogin(@RequestBody @Valid StoreLoginParam param){
        return authService.mallShopLogin(param.getPhone(),param.getPassword(), UserTypeEnum.STORE.getUserType(),1L);
    }

    @ApiOperation(value = "用户注销")
    @PostMapping(value = "/logout")
    public Response shopLogout(HttpServletRequest rq){
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        String md5= DigestUtils.md5((user.getUserType()+user.getUsername()));
        redisTemplate.delete(md5);
        return Response.success("注销成功");
    }

    @ApiOperation(value = "获取用户信息")
    @PostMapping(value = "/userInfo")
    public Response getUserInfo(HttpServletRequest rq){
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        String md5= DigestUtils.md5((user.getUserType()+user.getUsername()));
        Object obj=redisTemplate.opsForValue().get(user.getToken());
        TokenUsersVO userInfo=(TokenUsersVO)obj;
        Map<String,Object> map = new HashMap<>();
        map.put("name",userInfo.getRealName());
        map.put("company",userInfo.getStoreName());
        map.put("avatar","/images/face.jpg");
        List<String> roles = new ArrayList<>();
        roles.add("admin");
        map.put("roles",roles);
        map.put("introduction","这个人很懒,没有任何简介信息");
        return Response.data(map);
    }



}
